<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2019 广东卓锐软件有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------

namespace app\mill\model;

use think\Model;
use app\admin\model\Config as ConfigModel;
use app\mill\model\Order as OrderModel;
use app\mill\model\Give as GiveModel;
use app\mill\model\Sample as SampleModel;
use app\mill\model\Finance as FinanceModel;
use think\Db;

/**
 * 送货表模型
 */
class Address extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $name = 'mill_order_archives_address';

    // 自动写入时间戳
    protected $autoWriteTimestamp = true;

    //获取扫码人最近的发货单号
    public static function lastAddressSn($user = [],$give = []){

        $status = $user['give_sort'];
        $where = [
            ['status','=',$status],
            ['address_status','=',0],
            ['car_id','=',$give['car_id']],
            ['order_id','=',$give['order_id']],
            ['status_uid','=',$user['uid']],
        ];
        $address_sn = Db::name('mill_order_archives_give_log')->where($where)->order("status_time desc")->value('address_sn');

        return $address_sn;
    }
    // 表格制作
    //print 0:编辑模式 1:打印模式 2:导出模式
    public static function fromTabale($data = [],$print = 0){

        $address = $data['address'];

        //组装计价表单信息
        $table_arr = FinanceModel::fromArchivesTabale($data,1);
        $table_head = $table_arr['head'];
        $table_data = $table_arr['data'];
        $js = $table_arr['js'];
        $css = $table_arr['css'];

        //接着计价表单拼接发货信息
        $table_data[] = ["[14:1]"];
        $table_data[] = [
            "[14:1]<div class='dch-th'>发货备注</div>",
        ];
        $table_data[] = [
            "[14:1]<div style='text-align: left;font-weight: bold;'>".SampleModel::inputToText('note',$address['note'],$print,'height: 150px;','textarea')."</div>",
        ];

        $table_data[] = [
            "[2:1]<div class='dch-th'>发货员</div>",
            "[2:1]<div style='text-align: left;'>"
            .SampleModel::inputToText('id',$address['id'],$print,'','input','hidden')
            .SampleModel::inputToText('address_sn',$address['address_sn'],$print,'','input','hidden')
            .SampleModel::inputToText('order_pid',$address['order_pid'],$print,'','input','hidden')
            .SampleModel::inputToText('order_ids',$address['order_ids'],$print,'','input','hidden')
            .SampleModel::inputToText('archives_sns',$address['archives_sns'],$print,'','input','hidden')
            .SampleModel::inputToText('create_time',$address['create_time'],$print,'','input','hidden')
            .SampleModel::inputToText('user_nickname',$address['user_nickname'],$print,'width: 70%;display: inline;')."</div>",
            "[2:1]<div class='dch-th'>发货电话</div>",
            "[2:1]<div style='text-align: left;'>".SampleModel::inputToText('user_mobile',$address['user_mobile'],$print,'width: 70%;display: inline;')."</div>",
            "[2:1]<div class='dch-th'>发货日期</div>",
            "[4:1]<div style='text-align: left;'>".SampleModel::inputToText('address_date',$address['address_date'],$print,'width: 70%;display: inline;')."</div>",
        ];

        return ['head'=>$table_head,'data'=>$table_data,'js'=>$js,'css'=>$css];
    }

    // 打印模板
    //print 1:打印模式 2:导出模式
    public static function toHtml($data = [],$table = [],$print = 1){

        //缩放比例
        $table_scaling = 1;

        $html = '<html><style>th{font-size: 12px;font-weight: bold;}td{font-size: 12px;}</style><body style="margin: 4px;">';

        //$head_img = url_path(change_path(config('template.tpl_replace_string')['__ADMIN_IMG__'].'/logo-excel.jpg'));
        $head_img = config('template.tpl_replace_string')['__ADMIN_IMG__'].'/logo-excel.jpg';
        //头部
        //$html .= '<table border="1" style="text-align: center;border-collapse: collapse;width: '.(21/$table_scaling).'cm;transform-origin: 0 0;"><thead>
        /*$html .= '<table border="1" style="text-align: center;border-collapse: collapse;transform-origin: 0 0;"><thead>
                        <tr><th colspan="17" style="font-size: 25px;font-weight: bold;text-align:center;"><img style="width: 50px;margin-bottom: -20px;" class="td-none" src="'.config('template.tpl_replace_string')['__ADMIN_IMG__'].'/logo2.jpg">'.config('web_site_title').'</th></tr>*/
        $html .= '<table border="1" style="text-align: center;border-collapse: collapse;transform-origin: 0 0;"><thead>
                        <tr><th colspan="20" style="font-size: 18px;font-weight: bold;text-align:center;"><img style="margin-bottom: -20px;margin-left: -70px;width: 45px;" class="td-none" src="'.$head_img.'">'.config('web_site_title').'</th></tr>';
        $table_arr = self::fromTabale($data,$table,1);
        foreach ($table_arr as $tk=>$tv){
            //头
            if($tk == 'head'){
                foreach ($tv as $tvv){

                    $html .= '<tr>';
                    foreach ($tvv as $tvvv){

                        $v = OrderModel::tableParseCell($tvvv);

                        $html .= '
                            <th rowspan="'.$v['rowspan'].'" colspan="'.$v['colspan'].'">'.$v['value'].'</th>
                            ';
                    }
                    $html .= '</tr>';
                }
                $html .= '</thead>';
            }
            //体
            elseif($tk == 'data'){
                $html .= '<tbody>';
                foreach ($tv as $tvv){

                    $html .= '<tr>';
                    foreach ($tvv as $tvvv){

                        $v = OrderModel::tableParseCell($tvvv);

                        //$v['value'] = str_replace("\n","<br>",$v['value']);
                        $html .= '
                            <td rowspan="'.$v['rowspan'].'" colspan="'.$v['colspan'].'">'.$v['value'].'</td>
                            ';
                    }
                    $html .= '</tr>';
                }
                $html .= '</tbody>';
            }
        }
        $html .= '</table>';

        $js = 'window.print();';
        if($print == 2){
            $js = '$("table").table2excel({
                            // 不被导出的表格行的CSS class类
                            exclude: "",
                            // 导出的Excel文档的名称
                            name: "Excel Document Name",
                            // Excel文件的名称
                            filename: "发货单",
                            //文件后缀名
                            fileext: ".xls",
                            //是否排除导出图片
                            exclude_img: false,
                            //是否排除导出超链接
                            exclude_links: true,
                            //是否排除导出输入框中的内容
                            exclude_inputs: false
                        });';
        }
        //js
        $html .= '
                    <script src="'.config('template.tpl_replace_string')['__ADMIN_JS__'].'/core/jquery.min.js?v=20180327"></script>
                    <script src="'.config('template.tpl_replace_string')['__STATIC__'].'/common/js/jquery.table2excel.js?v='.date('Ymd').'"></script>
                    <script type="text/javascript">
                        //设置边框
                        //$(".td-border").parent().css("border","1px solid #777");
                        //去除边框
                        $(".td-none").parent().css("border-style","hidden")
                        //设置头部边框
                        $(".td-top").parent().css("border-top","black");
                        //设置底部边框
                        $(".td-bottom").parent().css("border-bottom","black");
                        //设置高度
                        $(".td-35").parent().css("height","35px");
                        $(".td-50").parent().css("height","25px");
                        //设置向下对齐
                        $(".text-bottom").parent().css("vertical-align","bottom");
                        //设置文字上下间距
                        $(".text-height").parent().css("line-height","28px");
                        $(".font-w-400").parent().css("font-weight","400");
                        //设置文字缩放比例
                        $("th,td").css("-webkit-transform","scale('.$table_scaling.')");
                        //设置文字缩放后左对齐
                        $(".scale-left").parent().css("transform-origin","0");
                        
                        '.$js.'
                    </script>';

        return $html;
    }

}
